package {
	import org.flixel.FlxSound;
	import org.flixel.FlxBasic;
	import jam.objects.Downer;
	import org.flixel.FlxState;
	import jam.objects.Line;
	import jam.registry.Resource;
	import org.flixel.FlxSprite;
	import org.flixel.FlxGroup;
	import org.flixel.FlxG;
	/**
	 * @author oati
	 */
	public class Boss extends FlxSprite {
		
		private var actionTimer:Number;
		// will take at least 2 seconds between actions
		private var baseActionTimer:Number = 3;
		// can take up to an additional 4 seconds between actions
		private var actionVariance:Number = 4;
		private var playState:PlayState;
		private var elapsedTime:Number = 0;
		private var jumpTime:Number = 0;
		private var thrownDowners:FlxGroup;
		private var thrownLines:FlxGroup;
		private var downer:FlxSprite;
		private var line:FlxSprite;
		private var actions:Array = new Array();
		private var faceplantSound:FlxSound;
		public var needsToDie:Boolean;
		public var fromLinePile:Boolean;
		public const BASE_JUMP_STRENGTH:int = 155;
		
		public function Boss(x:Number, y:Number, playState:FlxState) {
			//build actions
			super(x, y);
			this.playState = (playState as PlayState);
			thrownDowners = new FlxGroup();
			thrownLines = new FlxGroup();
			actions = new Array()[2];
			acceleration.y = Player.GRAVITY;
			drag.x = 20000;
			
			loadGraphic(Resource.FOEEXP, true, false, 48, 28);
			addAnimation("idle", [0]);
    		addAnimation("runLeft", [1, 2, 3, 4], 8, true);
    		addAnimation("runRight", [5, 6, 7, 8], 8, true);
			addAnimation("jump", [9]);
			addAnimation("death", [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 11, 11, 11, 11, 11, 11, 11, 11 ,11 ,11 ,11, 12, 13, 13, 13, 13, 13, 13, 13, 13], 15, false);
			width = 18;
			height = 24;
			centerOffsets();
			play("idle");
			
			faceplantSound = new FlxSound();
			faceplantSound.loadEmbedded(Resource.SOUND_FACE_PLANT);
			
			actionTimer = baseActionTimer;
		}
		
		override public function update():void {
			elapsedTime += FlxG.elapsed;
			jumpTime += FlxG.elapsed;
			
			if (elapsedTime >= actionTimer) {
				elapsedTime = 0;
				actionTimer = baseActionTimer * Math.floor(Math.random() * 3);
//				var action:int = Math.random()*actions.length;
				var action:int = Math.floor(Math.random() * 3);
				var horizontal:int = Math.floor(Math.random() * 81) + 20;
				
				switch (action) {
					case 0:
						break;
					case 1:
						line = new Line();
						line.x = x - 10;
						line.y = y + 5;
						line.velocity.y = -200;
						line.velocity.x = -horizontal;
						line.acceleration.y = Player.GRAVITY;
						line.solid = true;
						playState.lines.add(line);
						playState.add(line);
						break;
					case 2:
						downer = new Downer();
						downer.x = x - 10;
						downer.y = y + 5;
						downer.velocity.y = -200;
						downer.velocity.x = -horizontal;
						downer.acceleration.y = Player.GRAVITY;
						downer.solid = true;
						playState.downers.add(downer);
						playState.add(downer);
						break;	
						// etc
				}
			}
			if(jumpTime >= 10) {
				velocity.y = -BASE_JUMP_STRENGTH;
				jumpTime = 0;
			}
			
		}
	}
}
